package com.youlu.campus.admin.auth.controller;
import com.youlu.campus.admin.auth.entity.TenantInfo;
import com.youlu.campus.admin.auth.service.TenantInfoService;
import com.youlu.campus.admin.utils.AuthUtils;
import com.youlu.campus.common.base.RestResult;
import com.youlu.campus.common.base.RestResultBuilder;
import com.youlu.campus.entity.VO.req.TenantInfoQueryVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.repository.support.PageableExecutionUtils;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;

@RestController
public class TenantInfoController {
    @Autowired
    private TenantInfoService tenantInfoService;

    @PostMapping("/tenant/list")
    public RestResult getUsers(@RequestBody @Valid TenantInfoQueryVO req) {
        String comId = AuthUtils.getComId();
        Page<TenantInfo> users = tenantInfoService.list(req);
        if (users == null) {
            PageRequest pageRequest = PageRequest.of(req.getPage() - 1, req.getPageSize());
            users = PageableExecutionUtils.getPage(new ArrayList<>(), pageRequest, () -> 0L);
        }
        return RestResultBuilder.builder().success().data(users).build();
    }

    @PostMapping("/tenant/create")
    public RestResult create(@RequestBody @Valid TenantInfo req) {
        return RestResultBuilder.builder().success().data(tenantInfoService.create(req)).build();
    }

    @PostMapping("/tenant/update")
    public RestResult update(@RequestBody @Valid TenantInfo req) {
        return RestResultBuilder.builder().success().data(tenantInfoService.update(req)).build();
    }

    @GetMapping("/tenant/delete")
    public RestResult delete(@RequestParam("id") String id) {
        return RestResultBuilder.builder().success().data(tenantInfoService.delete(id)).build();
    }

    @GetMapping("/tenant/find-one")
    public RestResult findOne(@RequestParam("id") String id) {
        return RestResultBuilder.builder().success().data(tenantInfoService.findOne(id)).build();
    }
}
